﻿using System;
using System.Collections;
using System.IO;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Modules;
namespace Web.Cms.Modules
{
    public partial class ModuleControlEdit : AdminUserControl
    {
        private int _curModuleId;
        private int _curModuleDefinitionId;
        private ArrayList _lstFile = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            _curModuleDefinitionId = ConvertUtility.ToInt32(Request.QueryString["moduledefid"]);
            _curModuleId = ConvertUtility.ToInt32(Request.QueryString["moduleid"]);
            lblUpdateStatus.Text = string.Empty;
            if (!Page.IsPostBack)
            {
                dropModuleDef.DataSource = ModuleDefinitionController.GetModuleDefinitions(CurrentPortalID);
                dropModuleDef.DataTextField = "Title";
                dropModuleDef.DataValueField = "ModuleDefID";
                dropModuleDef.DataBind();

                ModuleDefinitionInfo _moduleDef = ModuleDefinitionController.GetModuleDefinition(_curModuleDefinitionId);
                ModuleInfo _module = ModuleController.GetModule(_curModuleId);
                if (_moduleDef != null)
                {
                    MiscUtility.SelectItemFromList(dropModuleDef, _curModuleDefinitionId.ToString());
                    LoadCtrlSource(Server.MapPath("/"), Server.MapPath(_moduleDef.Folder));
                    dropCtrlSource.DataSource = _lstFile;
                    dropCtrlSource.DataBind();
                }
                else if (_module != null)
                {
                    _moduleDef = ModuleDefinitionController.GetModuleDefinition(_module.ModuleDefID);
                    LoadCtrlSource(Server.MapPath("/"), Server.MapPath(_moduleDef.Folder));
                    //LoadCtrlSource(Server.MapPath("") + "/", Server.MapPath("") + _moduleDef.Folder);
                    dropCtrlSource.DataSource = _lstFile;
                    dropCtrlSource.DataBind();

                    txtModuleTitle.Text = _module.Title;
                    MiscUtility.SelectItemFromList(dropCtrlSource, _module.CtrlSrc);
                    txtCtrlKey.Text = _module.CtrlKey;
                    txtIconFile.Text = _module.IconFile;
                    MiscUtility.SelectItemFromList(dropModuleDef, _module.ModuleDefID.ToString());


                }

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleInfo _module = ModuleController.GetModule(_curModuleId);
                _module.Title = txtModuleTitle.Text;
                _module.ModuleDefID = ConvertUtility.ToInt32(dropModuleDef.SelectedValue);
                _module.CtrlKey = txtCtrlKey.Text;
                _module.CtrlSrc = dropCtrlSource.SelectedValue;
                _module.IconFile = txtIconFile.Text;
                ModuleController.UpdateModule(_module);
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = (ex.Message);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleInfo _module = new ModuleInfo();
                _module.Title = txtModuleTitle.Text;
                _module.ModuleDefID = ConvertUtility.ToInt32(dropModuleDef.SelectedValue);
                _module.CtrlKey = txtCtrlKey.Text;
                _module.CtrlSrc = dropCtrlSource.SelectedValue;
                _module.IconFile = txtIconFile.Text;
                ModuleController.CreateModule(_module);
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = (ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleController.DeleteModule(_curModuleId);
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = (ex.Message);
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "sys_moduledef", null, null));
        }
        private void LoadCtrlSource(string _rootPath, string _curPath)
        {
            string[] files = Directory.GetFiles(_curPath);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].EndsWith(".ascx"))
                {
                    string temp = files[i].Replace(_rootPath, string.Empty);
                    temp = temp.Replace("\\", "/");
                    if (!temp.StartsWith("/")) temp = "/" + temp;
                    _lstFile.Add(temp);
                }
            }
            string[] folders = Directory.GetDirectories(_curPath);
            for (int i = 0; i < folders.Length; i++)
            {
                if (folders[i] == Server.MapPath("/Cms")) continue;
                //if (folders[i] == Server.MapPath("/RadControls")) continue;
                //if (folders[i] == Server.MapPath("/Themes")) continue;
                //if (folders[i] == Server.MapPath("/Layouts")) continue;
                LoadCtrlSource(_rootPath, folders[i]);
            }
            _lstFile.Sort();
        }
    }
}